Software Development Exam  >  Software Development Questions  >  Consider the following JavaScript code:functi... Start Learning for Free
Consider the following JavaScript code:
function outer() {
  var x = 10;
  function inner() {
    console.log(x);
    var x = 20;
  }
  inner();
}
outer();
What will be the output of the above code?
  • a)
    10
  • b)
    20
  • c)
    Undefined
  • d)
    This code will produce an error
Correct answer is option 'C'. Can you explain this answer?
Most Upvoted Answer
Consider the following JavaScript code:function outer() { var x = 10; ...
Explanation:
When the above code is executed, it will output "undefined" in the console.

Reasoning:
To understand why "undefined" is the output, let's break down the code step by step:

1. The function outer() is defined.
2. Inside the outer() function, a variable x is declared and assigned a value of 10.
3. The function inner() is defined.
4. Inside the inner() function, a console.log(x) statement is executed. At this point, the variable x is still in the temporal dead zone (TDZ) since it has been declared but not yet assigned a value.
5. Next, the variable x is declared and assigned a value of 20. This assignment happens after the console.log(x) statement.
6. Finally, the inner() function is invoked.

Hoisting and Scoping:
The reason why the code outputs "undefined" instead of 10 is due to a concept called hoisting and the scoping rules in JavaScript.

- Hoisting: JavaScript moves all variable and function declarations to the top of their respective scopes during the creation phase. However, only the declarations are hoisted, not the assignments. In our code, the variable x is hoisted to the top of the inner() function, but its assignment of 20 is not hoisted.
- Scoping: JavaScript has function-level scoping. This means that variables declared inside a function are only accessible within that function, not outside. In our code, the variable x inside the inner() function shadows the variable x in the outer() function.

Order of Execution:
1. When the inner() function is invoked, the console.log(x) statement is executed. At this point, the variable x is in the TDZ and has not been assigned a value yet, so it is undefined.
2. After the console.log(x) statement, the variable x is assigned a value of 20.
3. Since the variable x inside the inner() function only exists within the function's scope, it does not affect the variable x in the outer() function.
4. Finally, when the outer() function is invoked, it calls the inner() function, which outputs "undefined" to the console.

Therefore, the correct answer is option C) Undefined.
Free Test
Community Answer
Consider the following JavaScript code:function outer() { var x = 10; ...
The code defines a function named "outer" that declares a variable "x" with the value 10. Inside "outer", there is a nested function named "inner" that logs the value of "x" to the console. However, before the console log, there is a variable declaration "var x = 20". This declaration creates a new local variable "x" within the "inner" function's scope. Since JavaScript has function-level scope, the declaration of the local "x" variable shadows the outer "x" variable. Therefore, when the console log is executed, the value of the local "x" variable, which is undefined, is logged.
Attention Software Development Students!
To make sure you are not studying endlessly, EduRev has designed Software Development study material, with Structured Courses, Videos, & Test Series. Plus get personalized analysis, doubt solving and improvement plans to achieve a great score in Software Development.
Explore Courses for Software Development exam

Top Courses for Software Development

Consider the following JavaScript code:function outer() { var x = 10; function inner() { console.log(x); var x = 20; } inner();}outer();What will be the output of the above code?a)10b)20c)Undefinedd)This code will produce an errorCorrect answer is option 'C'. Can you explain this answer?
Question Description
Consider the following JavaScript code:function outer() { var x = 10; function inner() { console.log(x); var x = 20; } inner();}outer();What will be the output of the above code?a)10b)20c)Undefinedd)This code will produce an errorCorrect answer is option 'C'. Can you explain this answer? for Software Development 2024 is part of Software Development preparation. The Question and answers have been prepared according to the Software Development exam syllabus. Information about Consider the following JavaScript code:function outer() { var x = 10; function inner() { console.log(x); var x = 20; } inner();}outer();What will be the output of the above code?a)10b)20c)Undefinedd)This code will produce an errorCorrect answer is option 'C'. Can you explain this answer? covers all topics & solutions for Software Development 2024 Exam. Find important definitions, questions, meanings, examples, exercises and tests below for Consider the following JavaScript code:function outer() { var x = 10; function inner() { console.log(x); var x = 20; } inner();}outer();What will be the output of the above code?a)10b)20c)Undefinedd)This code will produce an errorCorrect answer is option 'C'. Can you explain this answer?.
Solutions for Consider the following JavaScript code:function outer() { var x = 10; function inner() { console.log(x); var x = 20; } inner();}outer();What will be the output of the above code?a)10b)20c)Undefinedd)This code will produce an errorCorrect answer is option 'C'. Can you explain this answer? in English & in Hindi are available as part of our courses for Software Development. Download more important topics, notes, lectures and mock test series for Software Development Exam by signing up for free.
Here you can find the meaning of Consider the following JavaScript code:function outer() { var x = 10; function inner() { console.log(x); var x = 20; } inner();}outer();What will be the output of the above code?a)10b)20c)Undefinedd)This code will produce an errorCorrect answer is option 'C'. Can you explain this answer? defined & explained in the simplest way possible. Besides giving the explanation of Consider the following JavaScript code:function outer() { var x = 10; function inner() { console.log(x); var x = 20; } inner();}outer();What will be the output of the above code?a)10b)20c)Undefinedd)This code will produce an errorCorrect answer is option 'C'. Can you explain this answer?, a detailed solution for Consider the following JavaScript code:function outer() { var x = 10; function inner() { console.log(x); var x = 20; } inner();}outer();What will be the output of the above code?a)10b)20c)Undefinedd)This code will produce an errorCorrect answer is option 'C'. Can you explain this answer? has been provided alongside types of Consider the following JavaScript code:function outer() { var x = 10; function inner() { console.log(x); var x = 20; } inner();}outer();What will be the output of the above code?a)10b)20c)Undefinedd)This code will produce an errorCorrect answer is option 'C'. Can you explain this answer? theory, EduRev gives you an ample number of questions to practice Consider the following JavaScript code:function outer() { var x = 10; function inner() { console.log(x); var x = 20; } inner();}outer();What will be the output of the above code?a)10b)20c)Undefinedd)This code will produce an errorCorrect answer is option 'C'. Can you explain this answer? tests, examples and also practice Software Development tests.
Explore Courses for Software Development exam

Top Courses for Software Development

Explore Courses
Signup for Free!
Signup to see your scores go up within 7 days! Learn & Practice with 1000+ FREE Notes, Videos & Tests.
10M+ students study on EduRev